package com.client.view;


import com.client.bean.UserInfo;
import com.client.db.UserDbOper;
import org.w3c.dom.events.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.MouseEvent;
import java.io.*;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;


/**
 * Created by Administrator on 2016/12/16.
 */
public class LoginView extends JFrame implements ActionListener, ItemListener{

	JComboBox cbCode;
	JPasswordField txtpassword;
	JLabel lblface,lblReg/*,lblfindpassword*/;
//	JCheckBox cbmemeory,cbautologin;
	JButton btnLogin;

	public LoginView() throws HeadlessException {
//		this.setUndecorated(true);
		super("通讯客户端");
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Image icon = new ImageIcon("face/face_0.jpg").getImage();
		setIconImage(icon);

		JLabel lblhead = new JLabel(new ImageIcon("images/future.jpg"));
		add(lblhead);

		String codes[] = readCode();//读取曾经登录的帐号
		if (codes != null) {
			cbCode = new JComboBox(codes);
		} else
			cbCode = new JComboBox();
		txtpassword = new JPasswordField();
//		lblfindpassword = new JLabel("找回密码");
		lblReg = new JLabel("注册用户");

//		cbmemeory = new JCheckBox("记住密码");
//		cbautologin = new JCheckBox("自动登录");

		btnLogin = new JButton("登录");

		cbCode.setBounds(100,120,150,25);
		cbCode.setEditable(true);

		lblReg.setBounds(250,120,80,25);
		lblReg.setForeground(Color.BLUE);
		lblReg.setFont(new Font("宋体",Font.PLAIN , 12));

		txtpassword = new JPasswordField();
		txtpassword.setBounds(100,160,150,25);

//		lblfindpassword.setBounds(250,160,80,25);
//		lblfindpassword.setForeground(Color.BLUE);
//		lblfindpassword.setFont(new Font("宋体",Font.PLAIN , 12));

//		cbmemeory.setBounds(100,190,70,25);
//		cbmemeory.setForeground(Color.BLUE);
//		cbmemeory.setFont(new Font("宋体",Font.PLAIN , 12));
//
//		cbautologin.setBounds(180,190,70,25);
//		cbautologin.setForeground(Color.BLUE);
//		cbautologin.setFont(new Font("宋体",Font.PLAIN , 12));

		btnLogin.setBounds(100,220,150,25);
		btnLogin.setForeground(Color.BLUE);
		btnLogin.setFont(new Font("宋体",Font.PLAIN , 12));


		lblhead.add(cbCode);
		lblhead.add(lblReg);
		lblhead.add(txtpassword);
//		lblhead.add(lblfindpassword);
//		lblhead.add(cbmemeory);
//		lblhead.add(cbautologin);
		lblhead.add(btnLogin);

		lblface = new JLabel(new ImageIcon("face/logo.png"));
		lblface.setBounds(20,130,70,70);
		lblhead.add(lblface);

		btnLogin.addActionListener(this);
//		cbautologin.addItemListener(this);
//		cbmemeory.addItemListener(this);
		cbCode.addItemListener(this);

		//注册事件
		lblReg.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				new RegUser();
			}
		});

		//找回密码点击事件
//		lblfindpassword.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseClicked(MouseEvent e) {
//				JOptionPane.showMessageDialog(null,"帐号不合法");
//			}
//		});

		setSize(380,300);
		setVisible(true);
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new LoginView();
	}



	//登录点击事件
	@Override
	public void actionPerformed(ActionEvent e) {
		String code = cbCode.getSelectedItem().toString();
		String pwd  =new String(txtpassword.getPassword()).trim();
		UserInfo userInfo = new UserInfo();
		userInfo.setCode(Integer.parseInt(code));
		userInfo.setPassword(pwd);

		userInfo = new UserDbOper().login(userInfo);

		if (userInfo.getStatus() == 1) {//登录成功
			Icon ico = new ImageIcon(userInfo.getFace());
			lblface.setIcon(ico);
			saveCode(userInfo.getCode() + "");//把登录成功的帐号保存下来
			JOptionPane.showMessageDialog(null , "登录成功");
//			InetAddress addr = null;
//			try {
//				addr = InetAddress.getLocalHost();
//			} catch (Exception ex){
//				ex.printStackTrace();
//			}
//			System.out.println(addr.getHostAddress());
//			if (!addr.getHostAddress().equals(userInfo.getIpAddr())) {
//				JOptionPane.showMessageDialog(null,"请注意，此处不是常用登录");
//			}
			new MainView(userInfo);//显示主窗口
			this.dispose();//关闭登录窗口
		} else {
			JOptionPane.showMessageDialog(null,"帐号或密码错误");
		}
	}

	@Override
	public void itemStateChanged(ItemEvent e) {

		if (e.getSource() == cbCode) {
			String code = cbCode.getSelectedItem().toString();
			String face = new UserDbOper().getFace(Integer.parseInt(code));
			lblface.setIcon(new ImageIcon(face));
		}

	}

	/**
	 * 读曾经登录过的帐号
	 */
	public String[] readCode(){
		String[] codes = null;
		File file = new File("userInfo.dat");
		try {
			if (!file.exists()) {
				file.createNewFile();
			}

			BufferedReader br = new BufferedReader(new FileReader(file));
			String str = br.readLine();
			//把文件内容转换成数组
			br.close();
			if (str == null)
				return  null;
			codes = str.split(",");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return codes;
	}

	/**
	 * 创建保存帐号的文件
	 */
	public void saveCode(String code){
//		File file = new File("userInfo.dat");
		try {
//			if (!file.exists()) {
//
//				file.createNewFile();
//
//			}
//
//			BufferedReader br = new BufferedReader(new FileReader(file));
//			String str = br.readLine();
//			//把文件内容转换成数组
//			br.close();

			String[] codes = readCode();
			Set set = new TreeSet();
			String str = "";
			if (codes != null) {
				for (int i = 0; i < codes.length; i++) {
					set.add(codes[i]);
				}
				set.add(code);
				Iterator it = set.iterator();

				while (it.hasNext()) {
					str += it.next() + ",";
				}
			} else {
				str = code + "";
			}


			File f = new File("userInfo.dat");
			BufferedWriter bw = new BufferedWriter(new FileWriter(f));
			bw.write(str);
			bw.close();

		} catch (Exception e) {
			e.printStackTrace();
		}


	}

}
